/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.utils;

import com.redfin.sitemapgenerator.ChangeFreq;
import com.redfin.sitemapgenerator.W3CDateFormat;
import com.redfin.sitemapgenerator.W3CDateFormat.Pattern;
import com.redfin.sitemapgenerator.WebSitemapGenerator;
import com.redfin.sitemapgenerator.WebSitemapUrl;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Listing;
import org.cx1.model.Place;
import org.cx1.model.utils.KPlaceTypes;
import org.cx1.model.utils.SearchOption;
import org.cx1.model.utils.SearchResult;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IListingService;
import org.cx1.service.api.IPlaceService;
import org.cx1.service.api.ISearchService;
import org.cx1.service.impl.DListingService;
import org.cx1.service.impl.DPlaceService;
import org.cx1.service.impl.DSearchService;
import org.cx1.service.impl.MListingService;
import org.cx1.service.impl.MPlaceService;
import org.cx1.service.impl.MSearchService;

/**
 *
 * @author ECH
 */
public class SitemapGen {
    
    //private IListingService listingService = new MListingService();
    //private ISearchService searchService = new MSearchService();
    //private IPlaceService placeService = new MPlaceService();
    
    private IListingService listingService = new DListingService();
    private ISearchService searchService = new DSearchService();
    private IPlaceService placeService = new DPlaceService();
    
    private List<Listing> getListingsByCity(String city) {
        List<Listing> listings = new ArrayList();
        Set<Listing> uqListings = new HashSet<Listing>();
        
        ServiceResponse<String> sr3 = placeService.readIDByTitle(city);
        ServiceResponse<Place> sr4 = placeService.read(sr3.getResponse());
                
        List<Place> places = new ArrayList<Place>();
        Place plMakati = sr4.getResponse();
        places.add(plMakati);
        
        SearchOption so1 = new SearchOption();        
        so1.setLeaseTerm(1);
        so1.setPlaces(places);
        so1.setPage(1);
        so1.setItemsPerPage(9999999);
        
        ServiceResponse<SearchResult> sr5 = searchService.searchListings3(so1);
        if (sr5.getResponse() != null && sr5.getResponse().getListings() != null && sr5.getResponse().getListings().size() > 0) {  
            uqListings.addAll(sr5.getResponse().getListings());
        }
        
        SearchOption so2 = new SearchOption();        
        so2.setLeaseTerm(2);
        so2.setPlaces(places);
        so2.setPage(1);
        so2.setItemsPerPage(9999999);
        ServiceResponse<SearchResult> sr6 = searchService.searchListings3(so2);
        if (sr6.getResponse() != null && sr6.getResponse().getListings() != null && sr6.getResponse().getListings().size() > 0) {  
            uqListings.addAll(sr6.getResponse().getListings());
        } 
        
        listings.addAll(uqListings);        
        return listings;
    }
    
    public void generateSitemap() {
                
        List<Listing> listingsMakati = getListingsByCity("Makati");    
        List<Listing> listingsTaguig = getListingsByCity("Taguig");    
        List<Listing> listingsMandaluyong = getListingsByCity("Mandaluyong");    
        List<Listing> listingsPasig = getListingsByCity("Pasig");    
        List<Listing> listingsQuezonCity = getListingsByCity("Quezon City");    
        List<Listing> listingsManila = getListingsByCity("Manila");    
        List<Listing> listingsPasay = getListingsByCity("Pasay");    
        List<Listing> listingsParanaque = getListingsByCity("Paranaque");    
        List<Listing> listingsMuntinlupa = getListingsByCity("Muntinlupa");    
        List<Listing> listingsSanJuan = getListingsByCity("San Juan");    
        List<Listing> listingsCebu = getListingsByCity("Cebu");      
                
        ServiceResponse<List<Place>> response3 = placeService.readAllByType(KPlaceTypes.BUILDING.getId(), 0, 0);
        ServiceResponse<List<Place>> response4 = placeService.readAllByType(KPlaceTypes.VILLAGE.getId(), 0, 0);

        List<Place> buildings = response3.getResponse();
        List<Place> villages = response4.getResponse();
                         
        try {
            Date homepageUpdateDate = new Date();
            
            W3CDateFormat dateFormat = new W3CDateFormat(Pattern.DAY); 
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            WebSitemapGenerator wsg = WebSitemapGenerator.builder("http://rentpad.com.ph", new File(SystemProperties.SITEMAP_LOCATION)).dateFormat(dateFormat).build();  
            
            //CX: home page
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());
            
            //CX: search landing pages
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/long-term-rentals/makati").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());   
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/long-term-rentals/taguig").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/long-term-rentals/mandaluyong").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/long-term-rentals/pasig").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/long-term-rentals/quezon-city").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/long-term-rentals/manila").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/long-term-rentals/pasay").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/long-term-rentals/paranaque").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/long-term-rentals/muntinlupa").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/long-term-rentals/san-juan").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/long-term-rentals/cebu").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/short-term-rentals/makati").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build()); 
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/short-term-rentals/taguig").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/short-term-rentals/mandaluyong").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/short-term-rentals/pasig").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/short-term-rentals/quezon-city").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/short-term-rentals/manila").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/short-term-rentals/pasay").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/short-term-rentals/paranaque").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/short-term-rentals/muntinlupa").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/short-term-rentals/san-juan").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());  
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/short-term-rentals/cebu").lastMod(homepageUpdateDate).priority(1.0).changeFreq(ChangeFreq.DAILY).build());   
                
            //CX: place landing pages         
            wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/places").lastMod(homepageUpdateDate).priority(0.7).changeFreq(ChangeFreq.MONTHLY).build());
            if (buildings != null && buildings.size() > 0) {
                for (Place place : buildings) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/places/" + place.getTitle().toLowerCase().replaceAll(" ", "-") + "/" + place.getId()).lastMod(place.getDateModified()).priority(0.5).changeFreq(ChangeFreq.WEEKLY).build());
                }
            }
            if (villages != null && villages.size() > 0) {
                for (Place place : villages) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/places/" + place.getTitle().toLowerCase().replaceAll(" ", "-") + "/" + place.getId()).lastMod(place.getDateModified()).priority(0.5).changeFreq(ChangeFreq.WEEKLY).build());
                }
            }
                              
            //CX: individual listings, makati
            if (listingsMakati != null && listingsMakati.size() > 0) {
                for (Listing listing : listingsMakati) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/for-rent/" + "makati" + "/" + listing.getUrlTitle() + "/" + listing.getId()).lastMod(listing.getDateModified()).priority(0.5).changeFreq(ChangeFreq.MONTHLY).build());
                }
            }
            
            //CX: individual listings, taguig
            if (listingsTaguig != null && listingsTaguig.size() > 0) {
                for (Listing listing : listingsTaguig) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/for-rent/" + "taguig" + "/" + listing.getUrlTitle() + "/" + listing.getId()).lastMod(listing.getDateModified()).priority(0.5).changeFreq(ChangeFreq.MONTHLY).build());
                }
            }
            
            //CX: individual listings, mandaluying
            if (listingsMandaluyong != null && listingsMandaluyong.size() > 0) {
                for (Listing listing : listingsMandaluyong) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/for-rent/" + "mandaluyong" + "/" + listing.getUrlTitle() + "/" + listing.getId()).lastMod(listing.getDateModified()).priority(0.5).changeFreq(ChangeFreq.MONTHLY).build());
                }
            }
            
            //CX: individual listings, pasig
            if (listingsPasig != null && listingsPasig.size() > 0) {
                for (Listing listing : listingsPasig) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/for-rent/" + "pasig" + "/" + listing.getUrlTitle() + "/" + listing.getId()).lastMod(listing.getDateModified()).priority(0.5).changeFreq(ChangeFreq.MONTHLY).build());
                }
            }
            
            //CX: individual listings, quezon city
            if (listingsQuezonCity != null && listingsQuezonCity.size() > 0) {
                for (Listing listing : listingsQuezonCity) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/for-rent/" + "quezon-city" + "/" + listing.getUrlTitle() + "/" + listing.getId()).lastMod(listing.getDateModified()).priority(0.5).changeFreq(ChangeFreq.MONTHLY).build());
                }
            }
            
            //CX: individual listings, manila
            if (listingsManila != null && listingsManila.size() > 0) {
                for (Listing listing : listingsManila) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/for-rent/" + "manila" + "/" + listing.getUrlTitle() + "/" + listing.getId()).lastMod(listing.getDateModified()).priority(0.5).changeFreq(ChangeFreq.MONTHLY).build());
                }
            }
            
            //CX: individual listings, pasay
            if (listingsPasay != null && listingsPasay.size() > 0) {
                for (Listing listing : listingsPasay) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/for-rent/" + "pasay" + "/" + listing.getUrlTitle() + "/" + listing.getId()).lastMod(listing.getDateModified()).priority(0.5).changeFreq(ChangeFreq.MONTHLY).build());
                }
            }
            
            //CX: individual listings, paranaque
            if (listingsParanaque != null && listingsParanaque.size() > 0) {
                for (Listing listing : listingsParanaque) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/for-rent/" + "paranaque" + "/" + listing.getUrlTitle() + "/" + listing.getId()).lastMod(listing.getDateModified()).priority(0.5).changeFreq(ChangeFreq.MONTHLY).build());
                }
            }
            
            //CX: individual listings, muntinlupa
            if (listingsMuntinlupa != null && listingsMuntinlupa.size() > 0) {
                for (Listing listing : listingsMuntinlupa) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/for-rent/" + "muntinlupa" + "/" + listing.getUrlTitle() + "/" + listing.getId()).lastMod(listing.getDateModified()).priority(0.5).changeFreq(ChangeFreq.MONTHLY).build());
                }
            }
            
            //CX: individual listings, cebu
            if (listingsCebu != null && listingsCebu.size() > 0) {
                for (Listing listing : listingsCebu) {
                    wsg.addUrl(new WebSitemapUrl.Options("http://rentpad.com.ph/for-rent/" + "cebu" + "/" + listing.getUrlTitle() + "/" + listing.getId()).lastMod(listing.getDateModified()).priority(0.5).changeFreq(ChangeFreq.MONTHLY).build());
                }
            }
            
            wsg.write();
        } 
        catch (Exception ex) {
            Logger.getLogger(SitemapGen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void main(String[] argv) {
        SitemapGen sg = new SitemapGen();
        sg.generateSitemap();
    }
}
